package com.atguigu.order.service.impl;

import com.atguigu.commonutils.result.JWTUtils;
import com.atguigu.commonutils.result.vo.CourseWebVo;
import com.atguigu.commonutils.result.vo.UcenterMemberVo;
import com.atguigu.order.entity.PayLog;
import com.atguigu.order.feign.CourseFeignService;
import com.atguigu.order.feign.MemberFeignService;
import com.atguigu.servicebase.exception.MyException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.order.entity.Order;
import com.atguigu.order.service.OrderService;
import com.atguigu.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author kabuda
 * @description 针对表【t_order(订单)】的数据库操作Service实现
 * @createDate 2023-05-18 08:50:27
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {
    @Autowired
    private CourseFeignService courseFeignService;
    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public String saveOrder(String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> map = JWTUtils.checkToken(token);
        String id = (String) map.get("id");
        //幂等性判断

        if(isBuy(id,courseId)){
            throw new MyException(20001,"当前用户已购买此课程");
        }
        //获取课程信息
        CourseWebVo course = courseFeignService.getCourse(courseId);
        //获取会员信息
        UcenterMemberVo member = memberFeignService.getMember(id);

        Order order = new Order();
        String idStr = IdWorker.getIdStr();
        order.setOrderNo(idStr);
        order.setCourseId(courseId);
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        if(!StringUtils.isEmpty(member.getMobile())){
            order.setMobile(member.getMobile());
        }
        order.setMemberId(member.getId());
        order.setPayType(1);
        order.setStatus(0);
        order.setNickname(member.getNickname());
        order.setTeacherName(course.getTeacherName());
        order.setTotalFee(course.getPrice());
        if(!StringUtils.isEmpty(member.getOpenid())) {
            order.setMobile(member.getOpenid());
        }
        this.save(order);
        return order.getOrderNo();
    }

    @Override
    public boolean isBuy(String courseId,String memberId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getCourseId,courseId).eq(Order::getMemberId,memberId).eq(Order::getStatus,1);
        int count = this.count(wrapper);
        return count>0;
    }

}




